package org.point.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.point.common.service.impl.BaseServiceImpl;
import org.point.common.util.DoPage;
import org.point.dao.CreditsBehaviorDao;
import org.point.dao.CreditsTypeDao;
import org.point.dao.impl.CreditsRuleDaoImpl;
import org.point.domain.CreditsRule;
import org.point.domain.CreditsType;
import org.point.service.CreditsRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * @ClassName: CreditsRuleServiceImpl
 * @Description: TODO(积分规则业务实现类)
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午4:02:01
 *
 */
@Service("creditsRuleService")
public class CreditsRuleServiceImpl extends BaseServiceImpl<CreditsRule, Serializable> implements CreditsRuleService {

	@Autowired
	private CreditsRuleDaoImpl dao;
	
	@Autowired
	private CreditsTypeDao ctDao;
	
	@Autowired
	private CreditsBehaviorDao cbDao;
	
	public List<CreditsRule> searchCR(DoPage<CreditsRule> page) {
		List<CreditsRule> records = dao.findCRForMem(page);
		return records;
	}

	public DoPage<CreditsRule> searchCRForPage(DoPage<CreditsRule> page) {
		List<CreditsRule> records = dao.findCRForMem(page);
		Integer totalRecords = dao.countCRForMem(page);
		page.setList(records);
		page.setTotalRecords(totalRecords);
		return page;
	}

	public Integer countCR(DoPage<CreditsRule> page) {
		Integer result = dao.countCRForMem(page);
		return result;
	}

	public Integer insertCR(CreditsRule record, String crCode) {
		Integer result = 0;
		if(record.getId()!=null){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", record.getId());
			Integer isdel = dao.delCRbyId(map);
			Integer insert = dao.insertCR(record);
			if(isdel+insert>1){
				result = 1;
			}
		}else{
			record.setCreditsRuleId(crCode);
			result = dao.insertCR(record);
		}
		return result;
	}

	public Integer editCRStatus(Map<String, Object> map) {
		Integer result = dao.updStatus(map);
		return result;
	}

	public Integer deleteCRs(Map<String, Object> map) {
		Integer result = dao.delCRs(map);
		return result;
	}

	public CreditsRule getCRByCRID(Map<String, Object> map) {
		CreditsRule record = dao.searchForCT(map);
		return record;
	}

	public List<Hashtable<Object, Object>> selectCreditsTypes() {
		List<Hashtable<Object, Object>> record =  ctDao.searchSelectedInfo();
		return record;
	}

	public List<Hashtable<Object, Object>> selectcreditsBehavior() {
		List<Hashtable<Object, Object>> record = cbDao.searchSelectedInfo();
		return record;
	}

}
